<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="服务单号" prop="serviceNo">
        <el-input
          v-model="queryParams.serviceNo"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后类型" prop="serviceType">
        <el-select v-model="queryParams.serviceType" clearable size="small">
          <el-option
            v-for="item in serviceTypeOptions"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="售后状态" prop="serviceStatus">
        <el-select v-model="queryParams.serviceStatus" clearable size="small">
          <el-option
            v-for="item in serviceStatusOptions"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="createTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          :disabled="!selected"
          @click="handleAudit"
        >审核通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          :disabled="!selected"
          @click="handleReject"
        >审核拒绝</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" stripe border max-height="600" style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务单号" align="center" width="200" prop="orderNo" >
        <template slot-scope="scope">
          <router-link :to="{ path: '/service/service-info', query: {serviceNo: scope.row.serviceNo} } " class="link-type">
            <span>{{ scope.row.serviceNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" width="200" prop="orderNo" >
        <template slot-scope="scope">
          <router-link :to="{ path: '/order/order-info', query: {orderNo: scope.row.orderNo} } " class="link-type">
            <span>{{ scope.row.orderNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="会员账号" align="center" prop="uid" width="160" />
      <el-table-column label="手机号码" align="center" prop="phone" width="150" />
      <el-table-column label="售后类型" align="center" prop="serviceStatus">
        <template slot-scope="scope">
          <el-tag>{{serviceTypeFormat(scope.row.serviceType)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="售后状态" align="center" prop="serviceType">
        <template slot-scope="scope">
          <el-tag :type="serviceStatusTag(scope.row.serviceStatus)">{{serviceStatusFormat(scope.row.serviceStatus)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="applyReason" width="200" />
      <el-table-column label="申请描述" align="center" prop="applyContent" width="300" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180" />
      <el-table-column label="备注" align="center" prop="remark" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="修改人" align="center" prop="updateBy" width="60"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column fixed="right" label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleInfo(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { listService, auditService, rejectService } from "@/api/order/service";

  export default {
    name: "OrderList",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        serviceNos: [],
        selected: false,
        // 状态
        serviceStatusOptions: [],
        serviceTypeOptions: [],
        createTime: [],
        // 查询参数
        queryParams: {
          page: 1,
          size: 20,
          orderNo: undefined,
          serviceNo: undefined,
          serviceStatus: undefined,
          serviceType: undefined
        }
      };
    },
    created() {
      this.getList();
      this.getOptions("service-status").then(response => {
        this.serviceStatusOptions = response.data.options;
      });
      this.getOptions("service-type").then(response => {
        this.serviceTypeOptions = response.data.options;
      });
    },
    methods: {
      /** 查询商品类型列表 */
      getList() {
        this.loading = true;
        const payload = {
          ...this.queryParams,
          createTime: this.parseDataRange(this.createTime)
        };
        listService(payload).then(response => {
            this.list = response.data.rows;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      // 状态翻译
      serviceStatusTag(serviceStatus) {
        if (serviceStatus === 0) {
          return 'danger';
        } else if (serviceStatus === 1) {
          return 'warning';
        } else if (serviceStatus === 3) {
          return 'success';
        } else {
          return 'primary';
        }
      },
      serviceStatusFormat(data) {
        return this.selectOptionValue(this.serviceStatusOptions, data);
      },
      serviceTypeFormat(data) {
        return this.selectOptionValue(this.serviceTypeOptions, data);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.serviceNos = selection.map(item => item.serviceNo);
        this.selected = selection.length >= 1;
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.createTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查看详情 */
      handleInfo(row) {
        this.$router.push({path: '/service/service-info', query: {serviceNo: row.serviceNo}});
      },
      handleAudit() {
        this.$confirm('确定要审核通过选中的服务单吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          auditService({
            serviceNos: this.serviceNos
          }).then(res => {
            this.handleQuery();
            this.msgSuccess("审核成功");
          })
        });
      },
      handleReject() {
        this.$prompt('请输入原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern:  /[\S]+/,
          inputErrorMessage: '拒绝原因不能为空！'
        }).then(({ value }) => {
          rejectService({
            serviceNos: this.serviceNos,
            reason: value
          }).then(res => {
            this.handleQuery();
            this.msgSuccess("审核成功");
          })
        });
      }
    }
  };
</script>
